/*
 * Copyright 2012 Marsrutai.lt
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package lt.marsrutai.mobile.android.adapter;

import lt.marsrutai.mobile.android.AppContext;
import lt.marsrutai.mobile.android.R;
import lt.marsrutai.mobile.android.model.Schedule;
import lt.marsrutai.mobile.android.util.StringUtils;
import lt.marsrutai.mobile.android.widget.ListViewAdapter;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

public class ScheduleAdapter extends ListViewAdapter<Schedule> {

    private String scopeFolder;
    
    public ScheduleAdapter(Context context, String scopeFolder) {
        super(context, R.layout.schedule_item);
        
        this.scopeFolder = scopeFolder;
    }

    @Override
    public View doGetView(int position, View view, ViewGroup parent) {

        Schedule o = getItem(position);

        if (o != null) {
            
            view = inflater.inflate(R.layout.schedule_item, null);

            TextView scheduleTrackName = (TextView) view.findViewById(R.id.schedule_track_name);            

            TextView scheduleName = (TextView) view.findViewById(R.id.schedule_name);            
            scheduleName.setBackgroundColor(AppContext.getTransportInfoResolver().resolveTransportColor(scopeFolder, o.getTransportId()));
            
            if (isScheduleNameSignificant(o.getName(), o.getTrackName())) {
                scheduleTrackName.setText(o.getName());
            }
            else if (isScheduleTrackSignificant(o.getName(), o.getTrackName())) {
                scheduleTrackName.setText(o.getTrackName());
            }
            else {
                scheduleTrackName.setText(o.getTrackName());
                
                if (!StringUtils.equalsIgnoreCase(o.getName(), o.getTrackName())) {
                    scheduleName.setText(o.getName());
                }
            }
            
            TextView validComment = (TextView) view.findViewById(R.id.valid_comment);
            if (null != validComment) {
                if (null != o.getValidComment()) {
                    validComment.setText(activity.getString(R.string.is_valid) + ' ' + o.getValidComment());
                }
                else {
                    validComment.setVisibility(View.GONE);
                }
            }

        }
        return view;
    }

    /**
     * Indicates whether schedule name is more significant than schedule track
     * name, and should be used to represent the schedule
     * 
     * @param scheduleName
     * @param scheduleTrackName
     * @return
     */
    public static boolean isScheduleTrackSignificant(String scheduleName, String scheduleTrackName) {
        return scheduleTrackName != null && scheduleTrackName.contains(scheduleName);
    }

    /**
     * Indicates whether schedule track name is more significant than schedule
     * name, and should be used to represent the schedule
     * 
     * @param scheduleName
     * @param scheduleTrackName
     * @return
     */
    public static boolean isScheduleNameSignificant(String scheduleName, String scheduleTrackName) {
        return scheduleName != null && scheduleName.contains(scheduleTrackName);
    }

}
